#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <unordered_map>
#define maxn 1000
using namespace std;
class Solution
{
public:
    vector<string> stk;
    unordered_map<string, priority_queue<string, vector<string>, std::greater<string>>> vec;
    /**
     *  priority_queue<Type, Container, Functional> 
     */
    void dfs(const string &cur)
    {
        while (vec.count(cur) && vec[cur].size() > 0)
        {
            string tmp = vec[cur].top();
            vec[cur].pop();
            dfs(move(tmp));
        }
        stk.push_back(cur);
    };
    vector<string> findItinerary(vector<vector<string>> &tickets)
    {
        for (auto &it : tickets)
        {
            vec[it[0]].emplace(it[1]);
        }
        dfs("JFK");
        reverse(stk.begin(), stk.end());
        return stk;
    };
};